<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Gen
 * 
 * Install instructions: 
 * 
 * 1. download package and include in $autoload['packages']
 * 2. load library from controller action or autoload libraries
 * 3. run scaffold or build code
 * 
 * @author jeff@siccr.com
 *
 **/

class Gen {
	
	var $CI;
	var $table;
	var $ucf_controller;
	var $controller;
	var $model_name;
	
	var $base_uri;
	var $request;
	
	var $alias;
	
	public function __construct()
	{
		$this->CI =& get_instance();
		
		$this->CI->load->database();
		$this->CI->load->library('session');
		$this->CI->load->helper('form');
		$this->CI->load->helper('url');
	}
	
	
	/**
	 * 
	 * $this->load->library('gen');
	 * $this->gen->run('tableName');
	 * 
	 */
	function run($table,$alias=array()) {
		
		
		if (!$this->CI->db->table_exists($table)) {
			die('Table <strong>'.$table.'</strong> does not exist.');
		}
		
		$this->table = $table;
		$this->alias = $alias;

		/* Get rid of the CI default nonsense and set real path */
		$route =& load_class('Router');
		$base_url = $this->CI->config->site_url();
		if ($route->directory != '') { $base_url .= '/'; }
		
		$this->base_uri = $route->directory.'/'.$route->class.'/'.$route->method;

		/* Did we call a subfunction - catch it here */
		$segs = $this->CI->uri->segment_array();
		$last = array_search($route->method, $segs);	
		// Everything beyond this is ours
		
		$this->request = array('');
		if ($last < count($segs)) {
			$this->request = array_slice($segs, $last);
		}

		$this->_processRequest();

		exit; //Prevent loading of index function if we're in the constructor
	}
	
	
	/**
	 * Public Function
	 * 
	 * Starts the code generation process
	 * 
	 * $this->load->library('gen');
	 * $this->gen->code('tableName');
	 * 
	 */
	function code($table) {
		
		if (!$this->CI->db->table_exists($table)) {
			die('Table <strong>'.$table.'</strong> does not exist.');
		}
		
		$this->table = $table;
				
		/* Create model name based on table */
		$this->model_name = ucfirst(strtolower($this->table));
		
		/* Figure out the calling controller - that's the one we want to fix */
		$route =& load_class('Router');
		$this->controller = $route->class;
		$this->ucf_controller = ucfirst($route->class);
		
		$this->_generate();  //** FUNCTION FOUND BELOW (l.370) **//
	}
	
	/**
	 * Process any additional uri-segments
	 *
	 * If we have no extra segments we check if anything was submitted
	 */
	function _processRequest() {

		/* Check if something was submitted */
		$action = $this->CI->input->post('action');
		
		switch ($action) {
			case 'add':
				$this->_db_insert();
				break;
			case 'edit':
				$this->_db_edit();
				break;
			case 'delete':
				$this->_db_delete();
				break;
		}

		/* All forms submit to index, so we may be somewhere else */
		switch ($this->request[0]) {
			case 'show':
				$this->_dynamic('show');
				break;
			case 'add':
				$this->_dynamic('insert');
				break;
			case 'edit':
				$this->_dynamic('edit');
				break;
			case 'delete':
				$this->_dynamic('delete');
				break;
			default:
				// Nope, seems we really wanted index (or entered an invalid url);
				$this->_dynamic();
		}		
	}
	
	/*****								*****/
	/*****		PROCESS DB ACTIONS		*****/
	/*****								*****/
	
	function _db_insert() {
		unset($_POST['action']);
		unset($_POST['submit']);
		
		$this->CI->db->insert($this->table, $_POST);
		
		$this->CI->session->set_flashdata('msg', 'Entry Added');
		redirect($this->base_uri);
	}
	
	function _db_edit() {
		unset($_POST['action']);
		unset($_POST['submit']);
		
		$this->CI->db->where('id', $_POST['id']);
		$this->CI->db->update($this->table, $_POST);
		
		$this->CI->session->set_flashdata('msg', 'Entry Modified');
		redirect($this->base_uri);
	}
	
	function _db_delete($id) {
		$this->CI->db->where('id', $id);
		$this->CI->db->delete($this->table);

		$this->CI->session->set_flashdata('msg', 'Entry Deleted');
		redirect($this->base_uri);
	}
	
	
	/*****								*****/
	/*****		SHOW FORMS AND DATA		*****/
	/*****								*****/
	
    function _dynamic($action = 'list') {	//action comes from _processRequests
		
		switch ($action) {
			case 'list':
				$this->_list();
				break;
			case 'show':
				$this->_show();
				break;
			case 'insert':
				$this->_insert();
				break;
			case 'edit':
				$this->_edit();
				break;
			case 'delete':
				$this->_delete();				
			default:
				$this->_list();
		}
		
    }

	//Special case - here so that "Delete" can be a link instead of a button
	function _delete() {
		$id = $this->request[1];
		$this->_db_delete($id);
	}

	function _list() {		
		$query = $this->CI->db->get($this->table);
		$fields = $this->CI->db->list_fields($this->table);

		$this->_header();
		echo "<h1>List</h1>";

		$table = '<table><tr>';
		foreach ($fields as $field)
		   $table .= '<th>'.ucfirst($field).'</th>';
		$table.= '</tr>';

		foreach ($query->result_array() as $row)
		{
			$table.= '<tr>';
			foreach ($fields as $field)
			   $table.= '<td>'.$row[$field].'</td>';
			
			$table.= '<td>'.$this->_show_link($row['id']).'</td>'.
							'<td>'.$this->_edit_link($row['id']).'</td>'.
							'<td>'.$this->_delete_link($row['id']).'</td>';

			$table.= '</tr>';
		}
		$table.= '</table>';
		echo $table;
		
		echo $this->_insert_link();
		$this->_footer();
		
	}
	
	function _show() {
		echo '<h1>Show</h1>';
		
		$id = $this->request[1];
		$this->CI->db->where('id', $id);
		$query = $this->CI->db->get($this->table);
		
		$data = $query->result_array();
		
		foreach ($data[0] as $field_name => $field_value) {
		echo '<p>
			  <b>'.ucfirst($field_name).':</b>'.$field_value.'
			  </p>';
		}
		echo $this->_back_link();
	}

	function _insert() {
			
		$this->_header();
		echo '<h1>New</h1>';
		
		$fields = $this->CI->db->field_data($this->table);
		$form = form_open($this->base_uri);
		
		foreach($fields as $field) {
			$form .= $this->_insertMarkup($field);
		}

		$form .= form_hidden('action', 'add');
		$form .= '<br clear=all>'.form_submit('submit', 'Insert').'</p>';
		$form .= form_close();
		echo $form;
		
		echo $this->_back_link();
		$this->_footer();
	}
	
	function _edit() {
		
		$this->_header();
		echo '<h1>Edit</h1>';

		$id = $this->request[1];
		$this->CI->db->where('id', $id);
		$query = $this->CI->db->get($this->table);
		
		$data = $query->result_array();
		
		$fields = $this->CI->db->field_data($this->table);
		
		$form = form_open($this->base_uri);
		
		foreach($fields as $field) {
			$form .= $this->_editMarkup($field, $data[0]);
		}

		$form .= form_hidden('action', 'edit');
		$form .= '<p>'.$this->_back_link();
		$form .= form_submit('submit', 'Update').'</p>';
		$form .= form_close();
		echo $form;	
		$this->_footer();	
	}
	
	function _buildFkDropdown($field) {

		$name = $field->name;

		$fomr_select = '<select name="'.$field->name.'" title="'.$field->type.'('.$field->max_length.')'.'">';

		$select_value_label = 'id';

		$select_name_label = 'name';

		if((strrpos($name, '_id') > 0 && $field->type == 'int') || array_key_exists($name, $this->alias)) {
			
			if(array_key_exists($name, $this->alias)) {

				$name = $this->alias[$name];
			
			}
			
			$table = str_replace('_id', '', $name);

			if ($this->CI->db->table_exists($table)) {

				$fields = $this->CI->db->field_data($table);
				/*
				 * Search proper label field for dropdown
				 * do not use FK field containing 'id'
				 */
				foreach($fields as $field) {

					if(strlen(strrpos($field->name, 'id')) == 0) {

						$select_name_label = $field->name;
						break;
					}
				}
				/*
				 * Fill Dropdown with real data
				 */
				$list = $this->CI->db->get($table)->result();

				foreach($list as $dat) {
					 
					$fomr_select .= '<option value="'.$dat->{$select_value_label}.'">'.$dat->{$select_value_label}.'-'.$dat->{$select_name_label}.'</option>';
				}

				$fomr_select .= '</select>';

			} else {
					
				$fomr_select = 'no table';
			}

		} else {

			$fomr_select = '';
		}
		return $fomr_select;
	}
	
	/**
	* Dynamic Foreign key Forms
	*/
	
	function _insertMarkup($field) {
		
		$select = $this->_buildFkDropdown($field);
		
		if($select != '') {
			
			$form_markup  = '<div class="rowElem">';
			$form_markup .= form_label(ucfirst($field->name),$field->name);
			$form_markup .= $select.'</div>';
			return $form_markup;
			
		} else {
			
			return $this->_insertMarkupX($field);
		}
	
	}

	
	/**
	 * Dynamic Forms
	 */

	function _insertMarkupX($field) {
		if ($field->primary_key) {
			return '<input type="hidden" name="'.$field->name.'" value="" />';
		}
		
		else {
			
			$form_markup = "\n\t<div class='rowElem'>\n";
			$form_markup .= form_label(ucfirst($field->name),$field->name);
			$form_markup .= "\n\t";
		
			switch ($field->type) {
				case 'int':
					$form_markup .= form_input($field->name, '');
					break;
				case 'string':
					if($field->max_length == 1){
						$form_markup .= form_checkbox($field->name, 1);
					} else {
						$form_markup .= form_input($field->name, '');
					}
					break;
				case 'blob':
					$form_markup .= form_textarea($field->name, '');
					break;
				case 'datetime':
					$form_markup .= form_input($field->name, date("Y-m-d H:i:s"));
					break;
			}
		
			$form_markup .= "\t</div>\n";
			return $form_markup;
			
		}
		
	}
	
	function _editMarkup($field, $data) {
		if ($field->primary_key) {
			return '<input type="hidden" name="'.$field->name.'" value="'.$data[$field->name].'" />';
		}

		else {

			$form_markup = "\n\t<div class='rowElem'>\n";
			$form_markup .= '	<label for="'.$field->name.'">'.ucfirst($field->name).'</label>';
			$form_markup .= "<br/>\n\t";
		
			switch ($field->type) {
				case 'int':
					$form_markup .= form_input($field->name, $data[$field->name]);
					break;
				case 'string':
					$form_markup .= form_input($field->name, $data[$field->name]);
					break;
				case 'blob':
					$form_markup .= form_textarea($field->name, $data[$field->name]);
					break;
				case 'datetime':
					$form_markup .= form_input($field->name, $data[$field->name]);
					break;
			}
		
			$form_markup .= "\n\t</div>\n";
			return $form_markup;
			
		}
	}
	
	/*****								*****/
	/*****		HELPER FUNCTIONS		*****/
	/*****								*****/
	
	function _delete_link($id) {
		return anchor($this->base_uri.'/delete/'.$id, 'Delete', 'class="browse"');
	}
	
	function _edit_link($id) {
		return anchor($this->base_uri.'/edit/'.$id, 'Edit', 'class="browse"');
	}
	
	function _show_link($id) {
		return anchor($this->base_uri.'/show/'.$id, 'View', 'class="browse"');
	}
	
	function _insert_link() {
		return anchor($this->base_uri.'/add', 'New', 'class="browse"');
	}
	
	function _back_link() {
		return anchor($this->base_uri, 'Back', 'class="browse"');
	}
	
	function _header() {
		echo '<!DOCTYPE html>
				<html>
				<head>
				<meta charset="UTF-8">
				<title>Running - '.ucfirst($this->table).'</title><style>'.				
				$this->_load_style()
				.'</style><script type="text/javascript" src="http://www.dfc-e.com/vendors/jscripts/jquery.js"></script>
				<script type="text/javascript" src="http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/jquery.jqtransform.js"></script>
				</head>
				<body>
				<p style="color: green">'.$this->CI->session->flashdata('msg').'</p>'.
				'<section>';
	}
	
	function _footer() {
		echo '<script type="text/javascript">$("form").addClass("jqtransform").jqTransform();</script></section></body></html>';
	}
	
	
	/************************************** * * * * * **************************************/
	/**************************************           **************************************/
	/************************************** GENERATED **************************************/
	/**************************************           **************************************/
	/************************************** * * * * * **************************************/
	
	/**
	 * Main function for the generation process
	 *
	 * Computes file paths
	 * Calls part-generators
	 * Creates files
	 */
	function _generate() {

		$APPPATH = "media/";
		
		mkdir($APPPATH."models");
		mkdir($APPPATH."views");
		mkdir($APPPATH."controllers");
		/* Make crud model */
		
		echo "<h3>processing...$APPPATH</h3>";

		$model_path = $APPPATH.'models/'.$this->table.'.php';
		$model_text = $this->_generate_model();
		echo $model_path;
		file_put_contents($model_path, $model_text);
		echo $model_path.' created<br/>';
		
		/* Generate views for crud functions in subfolder */

		$view_folder = $APPPATH.'views/'.strtolower($this->controller);
		$view_text = $this->_generate_views();
		
		$dir_created = mkdir($view_folder);
		echo $dir_created ? $view_folder.' created<br/>' : $view_folder.' already exists - no need to create<br/>';
		
		foreach ($view_text as $view_name => $view) {
			$view_path = $view_folder.'/'.$view_name.'.php';
			
			file_put_contents($view_path, $view);
			echo $view_path.' created<br/>';
		}
		
		/* Create the controller to tie it all up */

		$controller_path = $APPPATH.'controllers/'.$this->ucf_controller.'.php';
		$controller_text = $this->_generate_controller();
		file_put_contents($controller_path, $controller_text);
		echo $controller_path.' created<br/>';
		
		echo '<br/>Scaffold completed.  Click '.anchor($this->controller, 'here').' to get started.';
	}



	function _update_folder_perm(){
		
		echo APPPATH;
		//system("chmod 777 ".APPPATH."");
	}
		
	/*****								*****/
	/*****			MODEL				*****/
	/*****								*****/
	
	/**
	 * Generates the model code
	 *
	 * Gets the user defined layout from the template
	 * Replaces all tags
	 * Calls _fix_indent for multi-line replacements
	 */
	function _generate_model() {
		
		$model_text = $this->_model_text();
		$fields = $this->CI->db->list_fields($this->table);
		
		/* REPLACE TAGS */
		$model_text = str_replace("{model_name}", $this->model_name, $model_text);
		$model_text = str_replace("{table}", $this->table, $model_text);
		
		
		/* Replace Variable Initialization */
		list($model_text, $indent) = $this->_fix_indent($model_text, 'variables');

		$var_init = '';
		foreach ($fields as $field) {
			$var_init .= $indent.'var $'.$field."	= '';\n";
		}
		$model_text = str_replace("{variables}\n", $var_init, $model_text);


		/* Replace Variable Setters */
		list($model_text, $indent) = $this->_fix_indent($model_text, 'set_variables_from_post');
		
		$var_set = '';
		foreach ($fields as $field) {
			$var_set .= $indent.'$this->'.$field.'	= $_POST[\''.$field."'];\n";
		}
		$model_text = str_replace("{set_variables_from_post}\n", $var_set, $model_text);


		return $model_text;
	}


	/*****								*****/
	/*****			VIEWS				*****/
	/*****								*****/
	
	/**
	 * Generates the View Files
	 *
	 * Grabs all of the view templates as defined in the array
	 * Replaces tags
	 */
	function _generate_views() {
		
		/* Template function = _<viewname>_view */
		$views = array('index', 'edit', 'list', 'new', 'show');

		$view_text = array();

		foreach ($views as $view) {
			$view_funct = '_'.$view.'_view';
			
			if (method_exists($this, $view_funct)) {
				//$view_text[$view] = $this->_header();
				$text = $this->$view_funct();
				$text = str_replace('{controller}', $this->controller, $text);
				$text = str_replace('{form_fields_create}', $this->_form_fields('create'), $text);
				$text = str_replace('{form_fields_update}', $this->_form_fields('update'), $text);
				$view_text[$view] = $text;
				//$view_text[$view] .= $this->_footer();
			}
		}
		
		return $view_text;
	}
	
	
	/*****								*****/
	/*****			CONTROLLER			*****/
	/*****								*****/
	
	/**
	 * Generates the controller
	 *
	 * Gets controller template
	 * Replaces tags
	 */
	function _generate_controller() {
		$text = $this->_controller_text();

		$text = str_replace('{ucf_controller}', $this->ucf_controller, $text);
		$text = str_replace('{controller}', $this->controller, $text);
		$text = str_replace('{model}', $this->model_name, $text);
		$text = str_replace('{view_folder}', strtolower($this->controller), $text);
		return $text;
	}
	
	
	/*****								*****/
	/*****		HELPER FUNCTIONS		*****/
	/*****								*****/
	
	/**
	 * Function to fix indentation for multi-line replacements
	 * 
	 * Cuts the indent off the tag and applies to to all lines
	 * that replace it.
	 */
	function _fix_indent($text, $tag) {
		$pattern = '/\n[\t ]*?\{'.$tag.'\}/';
		preg_match($pattern, $text, $matches);
		$indent = str_replace("\n", '', $matches[0]);
		$indent = str_replace('{'.$tag.'}', '', $indent);
		// Remove tag indent to fix first one
		$text = preg_replace($pattern, "\n{".$tag."}", $text);
		
		return array($text, $indent);
	}
	
	/**
	 * Gateway to markup functions
	 *
	 * Calls markup functions to create meta-type relevant fields
	 */
	function _form_fields($action) {
		
		$query = $this->CI->db->get($this->table);		
		$fields = $this->CI->db->field_data($this->table);
		$form = '';

		foreach($fields as $field) {
			if ($action == 'update')
				$form .= $this->_getEditMarkup($field);
			else
				$form .= $this->_getMarkup($field).$field->type;
		}
				
		return $form;
	}

	/**
	 * Creates form element for a given field
	 *
	 * Adds *NOW* to datetime field
	 * Indents elements for clean html
	 */
	function _getMarkup($field) {
		if ($field->primary_key) {
			return '<input type="hidden" name="'.$field->name.'" value="" />';
		}
		
		else {
		
		$form_markup = "\n\t<div class='rowElem'>\n";
		$form_markup .= '	<label for="'.$field->name.'">'.ucfirst($field->name).'</label>';
		$form_markup .= "<br/>\n\t";
		
		switch ($field->type) {
			case 'int':
				$form_markup .= form_input($field->name);
				break;
			case 'string':
				$form_markup .= form_input($field->name);
				break;
			case 'blob':
				$form_markup .= form_textarea($field->name);
				break;
			case 'datetime':
				$form_markup .= '<input type="text" name="'.$field->name.'" value="<?= date("Y-m-d H:i:s") ?>" maxlength="500" size="50"  />';
				break;
		}
		
		$form_markup .= "\t</div>\n";
		return $form_markup;
		
		}
	}
	
	/**
	 * Creates form elements for an existing row
	 *
	 * Adds existing data to each element
	 */
	function _getEditMarkup($field) {
		if ($field->primary_key) {
			return '<input type="hidden" name="'.$field->name.'" value=<?= $result["'.$field->name.'"]?> />';
		}
		
		else {
		
		$form_markup = "\n\t<div class='rowElem'>\n";
		$form_markup .= '	<label for="'.$field->name.'">'.ucfirst($field->name).'</label>';
		$form_markup .= "<br/>\n\t";
		
		switch ($field->type) {
			case 'int':
				$form_markup .= '<input type="text" name="'.$field->name.'" value="<?= $result["'.$field->name.'"]?>" maxlength="500" size="50" />';
				break;
			case 'string':
				$form_markup .= '<input type="text" name="'.$field->name.'" value="<?= $result["'.$field->name.'"]?>" maxlength="500" size="50"  />';
				break;
			case 'blob':
				$form_markup .= '<textarea name="'.$field->name.'" cols="90" rows="12" ><?= $result["'.$field->name.'"]?></textarea>';
				break;
			case 'datetime':
				$form_markup .= '<input type="text" name="'.$field->name.'" value="<?= $result["'.$field->name.'"]?>" maxlength="500" size="50"  />';
				break;
		}
		$form_markup .= "\n\t</div>\n";
		return $form_markup;
		
		}
	}
	
	/*****								*****/
	/*****			TEMPLATES			*****/
	/*****								*****/
	
	/**
	 * Controller Template - Tags:
	 *
	 * {model}						= model name
	 * {ucf_controller}				= UC controller name
	 * {controller}					= controller name formated for url
	 * {view_folder}				= name of the generated view folder
	 *
	 */
	
	function _controller_text() {
		return
			'<?php
			class {ucf_controller} extends CI_Controller {
			
				function {ucf_controller}() {
					parent::Controller();
					
					$this->load->database();
					$this->load->model(\'{model}\');
					$this->load->helper(\'url\');
					$this->load->helper(\'form\');
					$this->load->library(\'session\');
				}
					
				function index() {
					redirect(\'{controller}/show_list\');
				}
			
				function show_list() {
					$data[\'results\'] = $this->{model}->get_all();
					$this->load->view(\'{view_folder}/list\', $data);
				}
			
				function show($id) {
					$data[\'result\'] = $this->{model}->get($id);
					$this->load->view(\'{view_folder}/show\', $data);		
				}
			
				function new_entry() {
					$this->load->view(\'{view_folder}/new\');
				}
			
				function create() {
					$this->{model}->insert();
					
					$this->session->set_flashdata(\'msg\', \'Entry Created\');
					redirect(\'{controller}/show_list\');
				}
			
				function edit($id) {
					$res = $this->{model}->get($id);
					$data[\'result\'] = $res[0];
					$this->load->view(\'{view_folder}/edit\', $data);
				}
			
				function update() {
					$this->{model}->update();
					
					$this->session->set_flashdata(\'msg\', \'Entry Updated\');
					redirect(\'{controller}/show_list\');
				}
			
				function delete($id) {
					$this->{model}->delete($id);
					
					$this->session->set_flashdata(\'msg\', \'Entry Deleted\');
					redirect(\'{controller}/show_list\');
				}
			}';
	}
	
	/**
	 * Model Template - Tags:
	 *
	 * {model_name}					= $this->model_name (by default: ucfirst(strlower({table})))
	 * {table}						= table name
	 * {variables}					= variable initilizations
	 * {set_variables_from_post}	= all variables set equal to their POST counterparts
	 *
	 */
	
	function _model_text() {
		return
			'<?php
			class {model_name} extends CI_Model {
				{variables}
			
				function {model_name}() {
					parent::Model();
				}
			
				function insert() {
					{set_variables_from_post}
			
					$this->db->insert(\'{table}\', $this);
				}
			
				function get($id) {
					$query = $this->db->get_where(\'{table}\', array(\'id\' => $id));
					return $query->result_array();
				}
				
				function get_all() {
					$query = $this->db->get(\'{table}\');
					return $query->result_array();
				}
				
				function get_field_data() {
					return $this->db->field_data(\'{table}\');
				}
			
				function update() {
					{set_variables_from_post}
			
					$this->db->update(\'{table}\', $this, array(\'id\' => $_POST[\'id\']));
				}
			
				function delete($id) {
					$this->db->delete(\'{table}\', array(\'id\' => $id));
				}
			}';
	}
	
	/**
	 * View Templates
	 *
	 * {controller} = current controller
	 * {form_fields_create} = Empty form entry fields for all database fields
	 * {form_fields_update} = Filled in form entry fields for all database fields
	 */

	/* INDEX */
	function _index_view() {
		
		return 'You should not see this after scaffolding - index controller redirect by default.';
		
	}

	/* LIST */
	function _list_view() {
		return
			'<p style="color: green"><?= $this->session->flashdata(\'msg\') ?></p>
			
			<h1>List</h1>
			
			<table>
				<tr>
				<? foreach(array_keys($results[0]) as $key): ?>
					<th><?= ucfirst($key) ?></th>
				<? endforeach; ?>
				</tr>
			
			<? foreach ($results as $row): ?>
				<tr>
				<? foreach ($row as $field_value): ?>
					<td><?= $field_value ?></td>
				<? endforeach; ?>
					<td> <?= anchor("{controller}/show/".$row[\'id\'], \'View\') ?></td>
					<td> <?= anchor("{controller}/edit/".$row[\'id\'], \'Edit\') ?></td>
					<td> <?= anchor("{controller}/delete/".$row[\'id\'], \'Delete\') ?></td>
				</tr>
			<? endforeach; ?>
			</table>
			<br/>
			<?= anchor("{controller}/new_entry", "New") ?>';

	}
	
	/* SHOW */
	function _show_view() {
		return
			'<h1>Show</h1>
			
			<? foreach ($result[0] as $field_name => $field_value): ?>
			<p>
				<b><?= ucfirst($field_name) ?>:</b> <?= $field_value ?>
			</p>
			<? endforeach; ?>
			<?= anchor("{controller}/show_list", "Back") ?>';
	}

	/* EDIT */
	function _edit_view() {
		return 
			'<h1>Edit</h1>
			
			<?= form_open(\'{controller}/update\') ?>
			{form_fields_update}
			<p>
				<?= form_submit(\'submit\', \'Update\') ?>
			</p>
			<?= form_close() ?>
			<?= anchor("{controller}/show_list", "Back") ?>';
	}
	
	/* NEW */
	function _new_view() {
		return 
			'<h1>Newx</h1>
			
			<?= form_open(\'{controller}/create\') ?>
			{form_fields_create}
			<p>
				<?= form_submit(\'submit\', \'Create\') ?>
			</p>
			<?= form_close() ?>
			<br/>
			<?= anchor("{controller}/show_list", "Back") ?>';
	}
	
	function _load_style(){
			return '
					/*
					 * CRUD style box
					 * author: jeff@siccr.com
					 * 
					 */
					body {
						background-color: #a3c159;
					}
					h1, a, label, option, input {
						font-size: 0.9em;
						font-family: Arial, Helvetica, verdana sans-serif;
					}
					h1 {
						color: white;
						font-size: 1.5em;
					}
					a {
						border: 1px solid silver;						
						background-color: white;
						text-align: center;
						padding: 0;	
					}
					a.browse {
						width: 50px;
					}
					
					table {
						font-size: 0.9em;
						font-family: Arial, Helvetica, verdana sans-serif;
						background-color: #fff;
						border-collapse: collapse;
						width: 80%;
					}
					caption {
						font-size: 25px;
						color: #1ba6b2;
						font-weight: bold;
						text-align: left;	
						padding: 10px;
						margin-bottom: 2px;
					}
					th {
						border-right: 1px solid #fff;
						color: #6C8C37;
						text-align: center;
						padding: 2px;
						text-transform: uppercase;
						height: 25px;
						background-color: #ffffff;
						font-weight: normal;
					}
					tfoot {
						color: #1ba6b2;
						padding: 2px;
						text-transform: uppercase;
						font-size: 1.2em;
						font-weight: bold;
						margin-top: 6px;
						border-top: 6px solid #e9f7f6;
					}
					tbody tr {	
						background-color: #fff;
						border-bottom: 1px solid #f0f0f0;
						border-bottom: 1px solid silver;
					
					    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ffffff", endColorstr="#eeeeee"); /* for IE */
					    background: -webkit-gradient(linear, left bottom, left top, from(#eeeeee), to(#ffffff)); /* for webkit browsers */
					    background: -moz-linear-gradient(top,  #ffffff,  #eeeeee); /* for firefox 3.6+ */
						
						height: 44px;
					}
					tbody tr:hover {
						background: #FFF;
					}
					tbody td {
						color: #414141;
						padding: 5px;
						text-align: left;
					}
					tbody th {
						font-size: 1em;
						font-weight: bold;
						text-align: center;
						padding: 2px;						
					}
					tbody td a, tbody th a, a {
						color: #6C8C37;
						text-decoration: none;
						font-weight: bold;
						display: block;
					}
					tbody td a:hover, tbody th a:hover, a:hover{
						color: #009193;
						text-decoration: none;
					}
					form {
						margin: 0 auto;
						width: 50%;
						padding:10px;
						background-color: #fff;
						-webkit-border-radius: 10px;
						-moz-border-radius: 10px;
						border-radius: 10px;
					}
					
					/* -------------
					 * jqTransform 
					 * ------------- */
					
					form.jqtransformdone label{
						margin-top:4px;
						margin-right: 8px;
						display: block;
						float:left;
					}
					form.jqtransformdone div.rowElem{
						clear:both;
						padding:4px 0px;
					}
					/* -------------
					 * Buttons
					 * ------------- */
					button.jqTransformButton {
						margin: 0px;
						padding: 0px;
						border: none;
						background-color: transparent;
						cursor: pointer;
						overflow: visible;
						font-family: Arial;
					}
					
					*:first-child+html button.jqTransformButton[type]{width: 1;} /* IE7 */
					
					button.jqTransformButton span {
						background: transparent url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/btn_right.gif) no-repeat right top;
						display: block;
						float: left;
						padding: 0px 4px 0px 0px; /* sliding doors padding */
						margin: 0px;
						height: 33px;
					}
					
					button.jqTransformButton span span {
						background: transparent url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/btn_left.gif) no-repeat top left;
						color: #333;
						padding: 8px 4px 0px 8px;
						font-weight: normal;
						font-size: 12px;
						line-height: 13px;
						display: block;
						text-decoration: none;
						height: 33px;
					}
					
					/*hover*/
					button.jqTransformButton_hover span span { background-position: left -33px ; }
					button.jqTransformButton_hover span { background-position: right -33px ; }
					
					/*clicked*/
					button.jqTransformButton_click span span { background-position: left -66px ; }
					button.jqTransformButton_click span { background-position: right -66px ; }
					
					/* IE 6 */
					* html button.jqTransformButton { height:33px; }
					* button.jqTransformButton span span { height: 25px; }
					/* IE 7 */
					*+ html button.jqTransformButton { height:33px; }
					*+ button.jqTransformButton span span { height: 25px; }
					
					
					/* -------------
					 * Inputs
					 * ------------- */
					.jqTransformInputWrapper {
						background: transparent url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/input/input_text_left.gif) no-repeat left top;
						height: 31px;
						padding: 0px;
						float:left;
					}
					
					.jqTransformInputInner {
						background: transparent url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/input/input_text_right.gif) no-repeat top right;
						padding: 0px;
						margin: 0px;
					}
					
					.jqTransformInputInner div {
						height: 31px;
						margin:0px 0px 0px 8px;
					}
					
					.jqTransformInputInner div input {
						font-family:Arial, Helvetica, sans-serif;
						font-size:12px;
						line-height: 18px;
						vertical-align: middle;
						height: 31px;
						color:#404040;	
						border: none;
						padding: 8px 0px 0px 0px;
						margin: 0px;
						background:transparent;
					}
					
					/*  IE6 */
					* html .jqTransformInputInner div input {
						padding: 6px 0 0 0;
						margin: 0 0 0 -8px;
						height:24px;
					}
					/* Ie7 */
					*+ html .jqTransformInputInner div input {
						padding: 6px 0 0 0;
						margin-left:-10px;
						height:24px;
					}
					
					/*hover*/
					.jqTransformInputWrapper_hover{ background-position: left -31px ;}
					.jqTransformInputWrapper_hover div.jqTransformInputInner{ background-position: right -31px ;}
					
					/*focus*/
					.jqTransformInputWrapper_focus{ background-position: left -62px ;}
					.jqTransformInputWrapper_focus div.jqTransformInputInner{ background-position: right -62px ;}
					
					
					
					.jqTransformSafari .jqTransformInputInner div {
						position: relative;
						overflow: hidden;
						margin:0px 8px;
					}
					.jqTransformSafari .jqTransformInputInner div input {
						background-color: none;
						position: absolute;
						top: -10px;
						left: -2px;
						height: 42px; 
						padding-left: 4px;
					}
					
					
					
					/* -------------
					 * Textarea
					 * ------------- */
					table.jqTransformTextarea td#jqTransformTextarea-mm textarea{
						font-size: 12px;
						line-height: 16px;
					}
					
					table.jqTransformTextarea td{
					font-size: 1px;
					line-height: 1px;
					width: 5px;
					height: 5px;
					margin: 0;
					padding: 0;
					}
					table.jqTransformTextarea{}
					table.jqTransformTextarea tr{
					/*border: 1px solid red;*/
					
					}
					table.jqTransformTextarea textarea{
						margin: 0;
						padding: 0;
						border: 0;
						background: none;
					}
					table.jqTransformTextarea td#jqTransformTextarea-tl{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_tl.gif) no-repeat top left;}
					table.jqTransformTextarea td#jqTransformTextarea-tm{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_tm.gif) repeat-x top left;}
					table.jqTransformTextarea td#jqTransformTextarea-tr{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_tr.gif) no-repeat top left;}
					
					table.jqTransformTextarea td#jqTransformTextarea-ml{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_ml.gif) repeat-y top left;}
					table.jqTransformTextarea td#jqTransformTextarea-mm{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea-mm.gif) repeat;}
					table.jqTransformTextarea td#jqTransformTextarea-mr{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_mr.gif) repeat-y top left;}
					
					table.jqTransformTextarea td#jqTransformTextarea-bl{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_bl.gif) no-repeat top left;}
					table.jqTransformTextarea td#jqTransformTextarea-bm{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_bm.gif) repeat-x top left;}
					table.jqTransformTextarea td#jqTransformTextarea-br{background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea_br.gif) no-repeat top left;}
					
					/*hover*/
					table.jqTransformTextarea-hover td#jqTransformTextarea-tl{background-position:0px -5px;}
					table.jqTransformTextarea-hover td#jqTransformTextarea-tm{background-position:0px -5px;}
					table.jqTransformTextarea-hover td#jqTransformTextarea-tr{background-position:0px -5px;}
					table.jqTransformTextarea-hover td#jqTransformTextarea-ml{background-position:-5px 0px;}
					table.jqTransformTextarea-hover td#jqTransformTextarea-mm{background-image: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea-mm-hover.gif);}
					table.jqTransformTextarea-hover td#jqTransformTextarea-mr{background-position:-5px 0px;}
					table.jqTransformTextarea-hover td#jqTransformTextarea-bl{background-position:0px -5px;}
					table.jqTransformTextarea-hover td#jqTransformTextarea-bm{background-position:0px -5px;}
					table.jqTransformTextarea-hover td#jqTransformTextarea-br{background-position:0px -5px;}
					
					/*focus*/
					table.jqTransformTextarea-focus td#jqTransformTextarea-tl{background-position:0px -10px;}
					table.jqTransformTextarea-focus td#jqTransformTextarea-tm{background-position:0px -10px;}
					table.jqTransformTextarea-focus td#jqTransformTextarea-tr{background-position:0px -10px;}
					table.jqTransformTextarea-focus td#jqTransformTextarea-ml{background-position:-10px 0px;}
					table.jqTransformTextarea-focus td#jqTransformTextarea-mm{background-image: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/textarea/textarea-mm-focus.gif);}
					table.jqTransformTextarea-focus td#jqTransformTextarea-mr{background-position: -10px 0px;}
					table.jqTransformTextarea-focus td#jqTransformTextarea-bl{background-position:0px -10px;}
					table.jqTransformTextarea-focus td#jqTransformTextarea-bm{background-position:0px -10px;}
					table.jqTransformTextarea-focus td#jqTransformTextarea-br{background-position:0px -10px;}
					
					
					table.jqTransformTextarea .jqTransformSafariTextarea div {
						position: relative;
						overflow: hidden;
						border: 0px solid red;
					}
					table.jqTransformTextarea .jqTransformSafariTextarea textarea{
						background: none;
						position: absolute;
						top:-10px;
						left:-10px;
						padding: 10px 10px 10px 10px;
						resize: none;
					}
					
					
					/* -------------
					 * Radios
					 * ------------- */
					.jqTransformRadioWrapper {float: left;display:block;margin:0px 4px;	margin-top:5px;}
					.jqTransformRadio {
						background: transparent url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/radio.gif) no-repeat center top;
						vertical-align: middle;
						height: 19px;
						width: 18px;
						display:block;/*display: -moz-inline-block;*/
					}
					
					
					/* -------------
					 * Checkboxes
					 * ------------- */
					span.jqTransformCheckboxWrapper{
						display:block;float:left;
						margin-top:5px;
					}
					a.jqTransformCheckbox {
						background: transparent url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/checkbox.gif) no-repeat center top;
						vertical-align: middle;
						height: 19px;
						width: 18px;
						display:block;/*display: -moz-inline-block;*/
					}
					
					/* -------------
					 * Checked - Used for both Radio and Checkbox
					 * ------------- */
					a.jqTransformChecked { background-position: center bottom;}
					
					
					/* -------------
					 * Selects
					 * ------------- */
					.jqTransformSelectWrapper {
						width: 45px;
						position:relative;
						height: 31px;
						background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/select_left.gif) no-repeat top left;
						float:left;
					}
					
					.jqTransformSelectWrapper div span {
						
						font-size: 12px;
						float: none;
						position: absolute;
						width:14px;
						height: 31px;
						line-height: 15px;
						padding: 8px 0 0 7px;
						overflow: hidden;
						cursor:pointer;
						/*border: 1px solid #CCCCCC;*/
					/*	border-right: none;*/
					}
					
					.jqTransformSelectWrapper  a.jqTransformSelectOpen {
						display: block;
						position: absolute;
						right: 0px;
						width: 31px;
						height: 31px;
						background: url(http://www.dfc-e.com/sites/theme/jscripts/vendors/jqtransformplugin/img/select_right.gif) no-repeat center center;
					}
					
					.jqTransformSelectWrapper ul {
						position: absolute;
						width: 43px;
						top: 30px;
						left: 0px;
						list-style: none;
						background-color: #FFF;
						border: solid 1px #CCC;
						display: none;
						margin: 0px;
						padding: 0px;
						height: 150px;
						overflow: auto;
						overflow-y: auto;
						z-index:10;
					}
					
					.jqTransformSelectWrapper ul a {
						display: block;
						padding: 5px;
						text-decoration: none;
						color:#333;
						background-color: #FFF;
						font-size: 12px;
					}
					
					.jqTransformSelectWrapper ul a.selected {
						background: #EDEDED;
						color: #333;
					}
					
					.jqTransformSelectWrapper ul a:hover, .jqTransformSelectWrapper ul a.selected:hover {
						background:#3582c4;
						color: #fff;
					}
					
					/* -------------
					 * Hidden - used to hide the original form elements
					 * ------------- */
					.jqTransformHidden {display: none;}
					
					
					#ecrire a{
					    border-bottom: 0px;
					}
					.basicform .rowElem label{
					display: block;
					float: left;
					    width: 150px;
					}
					form.jqtransform .rowElem label{
					    width: 150px;
					}
					form.jqtransform .rowElem label.radiovalue{
					    width:30px;
					    display: block;
					    float: left;
					}
					form.jqtransform .rowElem label.checkboxvalue{
					    width:70px;
					    display: block;
					    float: left;
					    padding-left: 5px;
					}			
			
			';		
	}
	
}